package com.ruoyi.crop.controller;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import com.ruoyi.crop.api.domain.Crop;
import com.ruoyi.crop.api.domain.PlanClasses;
import com.ruoyi.crop.service.CropService;
import com.ruoyi.crop.service.PlanClassesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
@Slf4j
public class StatusController {
    @Autowired
    private CropService cropService;
    @Autowired
    private PlanClassesService planClassesService;
    /**
     * 3秒执行一次
     * @return
     * @throws Exception
     */
    @Scheduled(fixedDelay = 30 * 60 * 1000)
    public void send() {
        System.err.println("--------------------");
        List<Crop> list = cropService.list();
        for (Crop crop : list) {
            Date zzTime = crop.getZzTime();
            Date jsTime = crop.getJsTime();
            Date date = new Date();
            if(!(date.after(zzTime) && date.before(jsTime))){
                crop.setStatus(5);
                cropService.updateById(crop);
            }
        }
    }
    @Scheduled(fixedDelay = 30 * 60 * 1000)
    public void jiaGong() {
        System.err.println("+++++++++++++++++++++");
        List<PlanClasses> list = planClassesService.list();
        for (PlanClasses planClasses : list) {
            Date beginTime = planClasses.getBeginTime();
            Date endTime = planClasses.getEndTime();
            Date date = new Date();
            if(!(date.after(beginTime) && date.before(endTime))){
                planClasses.setDocumentStatus("2");
                planClassesService.updateById(planClasses);
            }
        }
    }
}
